﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Management_Nurse_Nurse : System.Web.UI.Page
{
    
    //protected void Page_Load(object sender, EventArgs e)
    //{
        
    //    if (!IsPostBack)
    //    {
    //        FillGender();
    //        FillMarital();
    //        LoadListWard();
    //        drdListWard.SelectedIndex = 0;
    //        loadListRoomByWard(int.Parse(drdListWard.SelectedValue));
    //        drdListRoom.SelectedIndex = 0;
    //        loadListBedByRoom(int.Parse(drdListRoom.SelectedValue));
    //        TabContainer1.TabIndex = 0;
    //    }
    //}
    //protected void drdListWard_SelectedIndexChanged(object sender, EventArgs e)
    //{
    //    // Load list the Room in ward
    //    loadListRoomByWard(int.Parse(drdListWard.SelectedValue));
    //    drdListRoom.SelectedIndex = 0;
    //    loadListBedByRoom(int.Parse(drdListRoom.SelectedValue));
        
    //}
    //protected void drdListRoom_SelectedIndexChanged(object sender, EventArgs e)
    //{
    //    loadListBedByRoom(int.Parse(drdListRoom.SelectedValue));
    //}
    ////============ Load Data for Dropdownlist =============//
    //private void LoadListWard()
    //{
    //    // Load list ward
    //    SqlConnection connection = new SqlConnection(GlobalConstant.CONNECTION_STRING);
    //    connection.Open();
    //    string sql = "SELECT ID, Name FROM [tbWard]";
    //    SqlCommand cmd = new SqlCommand(sql, connection);
    //    SqlDataReader reader = cmd.ExecuteReader();
    //    drdListWard.DataSource = reader;
    //    drdListWard.DataTextField = "Name";
    //    drdListWard.DataValueField = "ID";
    //    drdListWard.DataBind();
    //    connection.Close();
    //}
    //private void loadListRoomByWard(int IDWard)
    //{
    //    SqlConnection connection = new SqlConnection(GlobalConstant.CONNECTION_STRING);
    //    connection.Open();
    //    string sql = "SELECT ID, RoomName FROM [tbRoom] WHERE IDWard = " + IDWard;
    //    SqlCommand cmd = new SqlCommand(sql, connection);
    //    SqlDataReader reader = cmd.ExecuteReader();
    //    drdListRoom.DataSource = reader;
    //    drdListRoom.DataTextField = "RoomName";
    //    drdListRoom.DataValueField = "ID";
    //    drdListRoom.DataBind();
    //    connection.Close();
    //}
    //private void loadListBedByRoom(int IDRoom)
    //{
    //    SqlConnection connection = new SqlConnection(GlobalConstant.CONNECTION_STRING);
    //    connection.Open();
    //    string sql = "SELECT ID, BedName FROM [tbBed] LEFT JOIN tbImpatient ON tbBed.[ID]=tbImpatient.[IDBed] WHERE IDBed IS NULL AND IDRoom = " + IDRoom ;
    //    SqlCommand cmd = new SqlCommand(sql, connection);
    //    SqlDataReader reader = cmd.ExecuteReader();
    //    drdListBed.DataSource = reader;
    //    drdListBed.DataTextField = "BedName";
    //    drdListBed.DataValueField = "ID";
    //    drdListBed.DataBind();
    //    connection.Close();
    //}
    ////=========== End Load ===========================//
    //private void FillMarital()
    //{
    //    drHonNhan.Items.Add("Độc thân");
    //    drHonNhan.Items.Add("Đã lập gia đình");
    //    drHonNhan.Items.Add("Đã ly hôn");
    //}

    //private void FillGender()
    //{
    //    drdGender.Items.Add("Nam");
    //    drdGender.Items.Add("Nữ");
    //}
    //protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    //{
    //    tbHodem.Text = "";
    //    tbDiaChi.Text = "";
    //    tbPhone.Text = "";
    //    tbTen.Text = "";
    //}
    //protected void Button1_Click1(object sender, EventArgs e)
    //{
    //    //insert into tbPatient
    //    string sqlInsert1 = "INSERT INTO [tbPatient] "
    //            + "VALUES (@FirstName,@LastName,@Address,@Sex,@Tel,@Dob,@MaritalStatus,@DateReg,@NFullName,@NRelationShip,@NAddress,@NTel,@LFullName,@LClinicNo,@LAddress,@LTel) ";
    //    SqlConnection connection = new SqlConnection(GlobalConstant.CONNECTION_STRING);
    //    connection.Open();
    //    SqlCommand cmd = new SqlCommand(sqlInsert1, connection);
    //    cmd.CommandType = CommandType.Text;
    //    cmd.Parameters.AddWithValue("FirstName", tbHodem.Text);
    //    cmd.Parameters.AddWithValue("LastName", tbTen.Text);
    //    cmd.Parameters.AddWithValue("Address", tbDiaChi.Text);
    //    cmd.Parameters.AddWithValue("Sex", drdGender.SelectedIndex);
    //    cmd.Parameters.AddWithValue("Tel", tbPhone.Text);
    //    cmd.Parameters.AddWithValue("Dob", DatePicker1.getDateTime());
    //    cmd.Parameters.AddWithValue("MaritalStatus",drHonNhan.SelectedValue);
    //    cmd.Parameters.AddWithValue("DateReg", DatePicker2.getDateTime());
    //    cmd.Parameters.AddWithValue("NFullName", tbNName.Text);
    //    cmd.Parameters.AddWithValue("NRelationShip", tbNRelation.Text);
    //    cmd.Parameters.AddWithValue("NAddress", tbNAddress.Text);
    //    cmd.Parameters.AddWithValue("NTel", tbNPhone.Text);
    //    cmd.Parameters.AddWithValue("LFullName", tbLName.Text);
    //    cmd.Parameters.AddWithValue("LClinicNo", tbLCode.Text);
    //    cmd.Parameters.AddWithValue("LAddress", tbLAddress.Text);
    //    cmd.Parameters.AddWithValue("LTel", tbLPhone.Text);
    //    cmd.ExecuteNonQuery();
    //    connection.Close();

    //    // =============================================================//4
    //    SqlConnection conn = new SqlConnection(GlobalConstant.CONNECTION_STRING);
    //    conn.Open();
    //    String sql2 = "SELECT [ID] FROM [tbPatient] ORDER BY [ID]  DESC";
    //    SqlCommand cmd2 = new SqlCommand(sql2, conn);
    //    // Get ID 
    //    int IDPatient = (int) cmd2.ExecuteScalar();
    //    conn.Close();

    //    // Insert into tbImpatient
    //    SqlConnection connection3 = new SqlConnection(GlobalConstant.CONNECTION_STRING);
    //    connection3.Open();
    //    String sql3 = "INSERT INTO [tbImpatient]"
    //                + " VALUES (@IDpatient,@DayOnlist,@IDWard,@ExpectedDuration,@DateInWard,@DateExpectedLeave,@DateLeft,@IDBed)";
    //    SqlCommand cmd3 = new SqlCommand(sql3, connection3);
    //    cmd3.CommandType = CommandType.Text;
    //    cmd3.Parameters.AddWithValue("IDpatient", IDPatient);
    //    cmd3.Parameters.AddWithValue("DayOnlist", DatePicker2.getDateTime());
    //    cmd3.Parameters.AddWithValue("IDWard", int.Parse(drdListWard.SelectedValue));
    //    cmd3.Parameters.AddWithValue("ExpectedDuration", int.Parse(TextBox9.Text));
    //    DateTime time = DatePicker3.getDateTime();
    //    cmd3.Parameters.AddWithValue("DateInWard", time);
    //    cmd3.Parameters.AddWithValue("DateExpectedLeave", time.AddDays(double.Parse(TextBox9.Text)));
    //    cmd3.Parameters.AddWithValue("DateLeft", DatePicker4.getDateTime());
    //    cmd3.Parameters.AddWithValue("IDBed", int.Parse(drdListBed.SelectedValue));
    //    cmd3.ExecuteNonQuery();
    //    connection3.Close();
    //    // Update Bed  is used.
    //    SqlConnection connection4 = new SqlConnection(GlobalConstant.CONNECTION_STRING);
    //    connection4.Open();
    //    String sql4 = "UPDATE [tbBed] SET [IsUsed] = 1 WHERE [ID] = " + int.Parse(drdListBed.SelectedValue);
    //    SqlCommand cmd4 = new SqlCommand(sql4, connection4);
    //    cmd4.CommandType = CommandType.Text;
    //    cmd4.ExecuteNonQuery();
    //    connection4.Close();
    //    Response.Redirect("~/Management/Nurse/NurseManager.aspx");
    //}

    static int currentPatient;
    static int currentAppointment;

    protected void btFind_Click(object sender, EventArgs e)
    {
        if (!tbPatientID.Text.Trim().Equals(""))
        {
            benhnhanDS.SelectParameters["ID"].DefaultValue = tbPatientID.Text;
            currentPatient = Int32.Parse(tbPatientID.Text);
            GridView1.DataBind();
            if (GridView1.Rows.Count == 0)
            {
                btAddApp.Visible = false;
            }
            else
            {
                btAddApp.Visible = true;
            }
        }
    }
    protected void btAddPatientWithValue_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(GlobalConstant.CONNECTION_STRING);
        con.Open();
        string sql = "INSERT INTO tbPatient VALUES(@p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9)";
        SqlCommand cmd = new SqlCommand(sql, con);
        cmd.Parameters.Add("p1", SqlDbType.NVarChar).Value = tbFirstName.Text;
        cmd.Parameters.Add("p2", SqlDbType.NVarChar).Value = tbLastName.Text;
        cmd.Parameters.Add("p3", SqlDbType.NVarChar).Value = tbAddress.Text;
        cmd.Parameters.Add("p4", SqlDbType.Int).Value = Sex1.SelectedValue;
        cmd.Parameters.Add("p5", SqlDbType.NVarChar).Value = tbTel.Text;
        cmd.Parameters.Add("p6", SqlDbType.Date).Value = DatePicker1.getDateTime();
        cmd.Parameters.Add("p7", SqlDbType.NVarChar).Value = MaritalStatus1.SelectedValue;
        cmd.Parameters.Add("p8", SqlDbType.Int).Value = 1;
        cmd.Parameters.Add("p9", SqlDbType.Int).Value = 0;
        cmd.ExecuteNonQuery();
        
        //select last ID

        sql = "SELECT ID FROM tbPatient WHERE ID = IDENT_CURRENT('tbPatient')";
        cmd = new SqlCommand(sql, con);
        SqlDataReader reader = cmd.ExecuteReader();
        if (reader.Read())
        {
            int lastAddedPatient = reader.GetInt32(0);
            pnAddPatient.Visible = false;
            tbPatientID.Text = lastAddedPatient.ToString();
            btFind_Click(null,null);
        }
        reader.Close();
        con.Close();        
    }
    protected void btAddPatient_Click(object sender, EventArgs e)
    {
        tbFirstName.Text = "";
        tbLastName.Text = "";
        tbTel.Text="";
        tbAddress.Text = "";
        pnAddPatient.Visible = true;
    }
    protected void btAddApp_Click(object sender, EventArgs e)
    {
        tbLFullName.Text = "";
        tbLClinicNo.Text="";
        tbLTel.Text = "";
        Danhsachnhanvien1.loadAllStaff();
        pnKhambenh.Visible = true;
        btAddApp.Visible = false;
    }

    private void InsertAppointment()
    {
        SqlConnection con = new SqlConnection(GlobalConstant.CONNECTION_STRING);
        con.Open();
        string sql = "INSERT INTO tbAppointment VALUES(@p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8)";
        SqlCommand cmd = new SqlCommand(sql, con);
        cmd.Parameters.Add("p1", SqlDbType.Int).Value = currentPatient;
        cmd.Parameters.Add("p2", SqlDbType.Int).Value = Danhsachnhanvien1.getCurrentStaffID();
        cmd.Parameters.Add("p3", SqlDbType.Int).Value = Room1.SelectedValue;
        cmd.Parameters.Add("p4", SqlDbType.Date).Value = DateTime.Now;
        cmd.Parameters.Add("p5", SqlDbType.NVarChar).Value = tbLFullName.Text;
        cmd.Parameters.Add("p6", SqlDbType.NVarChar).Value = tbLClinicNo.Text;
        cmd.Parameters.Add("p7", SqlDbType.NVarChar).Value = tbLAddress.Text;
        cmd.Parameters.Add("p8", SqlDbType.NVarChar).Value = tbLTel.Text;
        cmd.ExecuteNonQuery();

        //current appointment
        sql = "SELECT ID From tbAppointment WHERE ID=IDENT_CURRENT('tbAppointment')";
        cmd = new SqlCommand(sql, con);
        SqlDataReader reader = cmd.ExecuteReader();
        if (reader.Read())
        {
            currentAppointment = reader.GetInt32(0);
        }
        reader.Close();

        con.Close();
    }

    protected void btImpatient_Click(object sender, EventArgs e)
    {
        InsertAppointment();
        tbExpectedDuration.Text = "";
        tbNFullName.Text = "";
        tbNRelationship.Text = "";
        tbNAddress.Text = "";
        tbNTel.Text = "";
        pnKhambenh.Visible = false;   
        pnNhapvien.Visible = true;
       
    }
    protected void btOutpatient_Click(object sender, EventArgs e)
    {
        InsertAppointment();

        SqlConnection con = new SqlConnection(GlobalConstant.CONNECTION_STRING);
        con.Open();
        
        string sql = "UPDATE tbPatient SET Status=0, LastAppointment=" + currentAppointment + " WHERE ID=" + currentPatient + "; " +
              "INSERT INTO tbOutpatient VALUES(@p1, @p2, @p3)";
        SqlCommand cmd = new SqlCommand(sql, con);
        cmd.Parameters.Add("p1", SqlDbType.Int).Value = currentPatient;
        cmd.Parameters.Add("p2", SqlDbType.Int).Value = currentAppointment;
        cmd.Parameters.Add("p3", SqlDbType.Date).Value = DateTime.Now;
        cmd.ExecuteNonQuery();
        
        con.Close();
        pnKhambenh.Visible = false;        
    }
    protected void btSave_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(GlobalConstant.CONNECTION_STRING);
        con.Open();

        string sql = "UPDATE tbPatient SET Status=0, LastAppointment=" + currentAppointment + " WHERE ID=" + currentPatient + "; "
            + "INSERT INTO tbImpatient VALUES(@p01, @p02, @p03, @p04, @p05, @p06, @p07, @p08, @p09, @p10, @p11, @p12)";
        SqlCommand cmd = new SqlCommand(sql, con);
        cmd.Parameters.Add("p01", SqlDbType.Int).Value = currentPatient;
        cmd.Parameters.Add("p02", SqlDbType.Int).Value = currentAppointment;
        cmd.Parameters.Add("p03", SqlDbType.Int).Value = Ward1.SelectedValue;
        cmd.Parameters.Add("p04", SqlDbType.Date).Value = DateTime.Now;
        cmd.Parameters.Add("p05", SqlDbType.Int).Value = tbExpectedDuration.Text;
        cmd.Parameters.Add("p06", SqlDbType.Date).Value = DatePicker2.Date;
        cmd.Parameters.Add("p07", SqlDbType.Date).Value = DBNull.Value;
        cmd.Parameters.Add("p08", SqlDbType.Int).Value = 0;
        cmd.Parameters.Add("p09", SqlDbType.NVarChar).Value = tbNFullName.Text;
        cmd.Parameters.Add("p10", SqlDbType.NVarChar).Value = tbNRelationship.Text;
        cmd.Parameters.Add("p11", SqlDbType.NVarChar).Value = tbNAddress.Text;
        cmd.Parameters.Add("p12", SqlDbType.NVarChar).Value = tbNTel.Text;

        cmd.ExecuteNonQuery();

        con.Close();
        pnNhapvien.Visible = false;
    }
}